/**
 * ServerConnector.java
 * FlightBookingCentre
 * Phillip
 * 2014��3��22�� ����2:01:50
 */
package com.controller.broker;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import com.HOPP.ServerHOPP;
import com.dao.Flight;
import com.util.Constant;

/**
 * @author Phillip
 * 
 */
public class BrokerCHOPP implements ServerHOPP {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	private int serverName;
	private int PORT;
	private InetAddress address = null;
	private Socket socket = null;
	private BufferedReader reader = null;
	private PrintStream writer = null;

	public BrokerCHOPP(int serverName) {
		this.serverName = serverName;
		PORT = 10010 + serverName;
		try {
			address = InetAddress.getLocalHost();
			socket = new Socket(address, PORT);
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			writer = new PrintStream(socket.getOutputStream());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public void sendBYE() {
		writer.print("BYE" + Constant.CR_LF);
	}

	@Override
	public List<String> queryCity() {
		List<String> resultStrings = new ArrayList<String>();
		writer.print("0" + Constant.CR_LF);
		try {
			String buff = reader.readLine();
			while (!buff.equals(Constant.EOS_STRING)) {
				resultStrings.add(buff);
				buff = reader.readLine();
			}
			return resultStrings;
		} catch (IOException e) {
			e.printStackTrace();
			return resultStrings;
		}
	}

	@Override
	public List<Flight> queryFlights(String deptString, String destString, String deptDateString, String returnDateString) {
		List<Flight> result = new ArrayList<Flight>();
		Flight tempFlight;
		writer.print("10" + Constant.CR_LF);
		writer.print(deptString + Constant.CR_LF);
		writer.print(destString + Constant.CR_LF);
		writer.print(deptDateString + Constant.CR_LF);
		writer.print(returnDateString + Constant.CR_LF);
		String buff = "";
		try {
			buff = reader.readLine();
			if (buff.equals("10")) {
				return result;
			} else if (buff.equals("11")) {

				while (true) {
					buff = reader.readLine();
					if (buff.equals(Constant.EOS_STRING)) {
						break;
					}
					tempFlight = new Flight();
					tempFlight.setAirlineString(buff);
					buff = reader.readLine();
					tempFlight.setFligthNOString(buff);
					buff = reader.readLine();
					tempFlight.setDeptCityString(buff);
					buff = reader.readLine();
					tempFlight.setDeptDate(Date.valueOf(buff));
					buff = reader.readLine();
					tempFlight.setArriDate(Date.valueOf(buff));
					buff = reader.readLine();
					tempFlight.setDeptTime(Time.valueOf(buff));
					buff = reader.readLine();
					tempFlight.setArriTime(Time.valueOf(buff));
					buff = reader.readLine();
					tempFlight.setDeptAirportString(buff);
					buff = reader.readLine();
					tempFlight.setArriAirportString(buff);
					buff = reader.readLine();
					tempFlight.setPlaneString(buff);
					buff = reader.readLine();
					tempFlight.setClassEnum(buff);
					buff = reader.readLine();
					tempFlight.setPriceString(buff);
					result.add(tempFlight);

				}
				return result;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}
		return null;
	}

	@Override
	public String book(int airline, String flightNoString1, String deptString, String classString1, String flightNoString2, String returnString,
			String classString2, String nameString, String creditString) {
		String resultString = "";
		writer.print("2" + Constant.CR_LF);
		writer.print(flightNoString1 + Constant.CR_LF);
		writer.print(deptString + Constant.CR_LF);
		writer.print(classString1 + Constant.CR_LF);
		writer.print(flightNoString2 + Constant.CR_LF);
		writer.print(returnString + Constant.CR_LF);
		writer.print(classString2 + Constant.CR_LF);
		writer.print(nameString + Constant.CR_LF);
		writer.print(creditString + Constant.CR_LF);
		String buff = "";
		try {
			buff = reader.readLine();
			if (!buff.equals("2")) {
				return null;
			} else {
				resultString = reader.readLine();
				buff = reader.readLine();
				return resultString;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return resultString;
		}
	}

	@Override
	public String queryRates(int airline, String flightString, String deptDateString, String classString) {
		writer.print("12" + Constant.CR_LF);
		writer.print(flightString + Constant.CR_LF);
		writer.print(deptDateString + Constant.CR_LF);
		writer.print(classString + Constant.CR_LF);
		String buff = "";
		try {
			buff = reader.readLine();
			if (buff.equals("12")) {
				buff = reader.readLine();
				return buff;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
		return null;
	}

	@Override
	public String queryAirline(String deptString, String dsctString) {

		writer.print("13" + Constant.CR_LF);
		writer.print(deptString + Constant.CR_LF);
		writer.print(dsctString + Constant.CR_LF);

		try {
			return reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
            switch (this.serverName) {
                case 1:
                    return Constant.NOTSUPPORTED_STRING + "Air China";
                case 2:
                    return Constant.NOTSUPPORTED_STRING + "Qantas";
                case 3:
                    return Constant.NOTSUPPORTED_STRING + "China Eastern";
            }
            return null;
		}
	}
}
